#include "camera.h"


Camera::Camera(QObject *parent)
    : QThread{parent},m_video_capturer{new cv::VideoCapture}
{
qRegisterMetaType<cv::Mat>("cv::Mat");
//     qRegisterMetaType<Mat>("Mat");
}

bool Camera::openCamea(const QString &device_name)
{
    bool bRet = m_video_capturer->open(device_name.toStdString().data());
    if(bRet){
        // 初始化相机
        cv::VideoWriter vw;
        auto fourcc = vw.fourcc('M', 'J', 'P', 'G');
        m_video_capturer->set(cv::CAP_PROP_FRAME_WIDTH, 1920);
        m_video_capturer->set(cv::CAP_PROP_FRAME_HEIGHT, 1080);
        m_video_capturer->set(cv::CAP_PROP_FOURCC, fourcc);
        m_video_capturer->set(cv::CAP_PROP_FPS,60);
        start_capture();
    }

    return bRet;
}

void Camera::stop()
{
    this->m_loop.store(false);
}

void Camera::run()
{
    while (this->m_loop.load()) {
        capture();
    }
}

void Camera::start_capture()
{
    this->m_loop.store(true);
    this->start();
}

void Camera::stop_capture()
{
    this->m_loop.store(false);
}

void Camera::capture()
{
    cv::Mat frame;
   *m_video_capturer >> frame;
    emit appending_frame(frame);
}
